Home:ALL Converter>Python ping script

Python ping script

Ask Time:2018-06-14T04:55:26         Author:Melissa Chillington

Json Formatter

I am trying to write a Python script that pings IP addresses and outputs whether each ping succeeded. So far I have the following code, but the output seems inaccurate. Namely, when I run the script, it pings each hostname as expected but the output is only ever all up or all down.

import os

hostname0 = "10.40.161.2"
hostname1 = "10.40.161.3"
hostname2 = "10.40.161.4"
hostname3 = "10.40.161.5"

response = os.system("ping -c 1 " + hostname0)
response = os.system("ping -c 1 " + hostname1)
response = os.system("ping -c 1 " + hostname2)
response = os.system("ping -c 1 " + hostname3)

if response == 0:
    print hostname0, 'is up'
    print hostname1, 'is up'
    print hostname2, 'is up'
    print hostname3, 'is up'
else:
    print hostname0, 'is down'
    print hostname1, 'is down'
    print hostname2, 'is down'
    print hostname3, 'is down'

Author:Melissa Chillington,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50846131/python-ping-script
yy